# -*- coding: utf-8 -*-
"""
TgTable列颜色风格演示
演示如何使用TgTable的各种列颜色风格功能
"""

import sys
import os
import random
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QLabel
from PySide6.QtCore import Qt

# 添加项目根目录到Python路径
current_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(os.path.dirname(os.path.dirname(current_dir)))
sys.path.insert(0, project_root)

from ui.components.composite.table import TgTable, TgTableColumnStyle

class TableColorStyleDemo(QMainWindow):
    """TgTable列颜色风格演示窗口"""
    
    def __init__(self):
        super().__init__()
        self.setWindowTitle("TgTable列颜色风格演示")
        self.setGeometry(100, 100, 1200, 800)
        
        # 创建中央部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 创建布局
        layout = QVBoxLayout(central_widget)
        
        # 创建演示数据
        self.create_demo_tables(layout)
    
    def create_demo_tables(self, layout):
        """创建各种风格的演示表格"""
        
        # 1. 盈亏风格演示
        layout.addWidget(QLabel("1. 盈亏风格演示（正数红色，负数绿色）"))
        profit_loss_table = self.create_profit_loss_demo()
        layout.addWidget(profit_loss_table)
        
        # 2. 性能风格演示
        layout.addWidget(QLabel("2. 性能风格演示（正数绿色，负数红色）"))
        performance_table = self.create_performance_demo()
        layout.addWidget(performance_table)
        
        # 3. 热力图风格演示
        layout.addWidget(QLabel("3. 热力图风格演示（红色热力图）"))
        heatmap_table = self.create_heatmap_demo()
        layout.addWidget(heatmap_table)
        
        # 4. 彩虹渐变风格演示
        layout.addWidget(QLabel("4. 彩虹渐变风格演示"))
        gradient_table = self.create_gradient_demo()
        layout.addWidget(gradient_table)
    
    def create_profit_loss_demo(self):
        """创建盈亏风格演示表格"""
        headers = ['股票代码', '股票名称', '今日涨跌', '涨跌幅(%)', '成交量']
        data = [
            ['000001', '平安银行', '+1.25', '+2.15', '125万'],
            ['000002', '万科A', '-0.65', '-1.85', '89万'],
            ['000858', '五粮液', '+5.20', '+3.42', '156万'],
            ['600036', '招商银行', '-2.10', '-0.98', '234万'],
            ['600519', '贵州茅台', '+15.60', '+0.85', '67万'],
        ]
        
        table = TgTable(headers=headers, data=data, auto_height=True)
        
        # 设置盈亏风格到涨跌列（索引2）和涨跌幅列（索引3）
        table.set_profit_loss_style(2, background_color=True, text_color=False)
        table.set_profit_loss_style(3, background_color=False, text_color=True)
        
        return table
    
    def create_performance_demo(self):
        """创建性能风格演示表格"""
        headers = ['指标名称', '本月表现', '季度表现', '年度表现']
        data = [
            ['收益率', '5.2%', '-2.1%', '12.8%'],
            ['夏普比率', '1.35', '0.85', '1.42'],
            ['最大回撤', '-3.2%', '-8.5%', '-12.1%'],
            ['胜率', '68%', '72%', '65%'],
            ['盈亏比', '2.1', '1.8', '2.3'],
        ]
        
        table = TgTable(headers=headers, data=data, auto_height=True)
        
        # 设置性能风格到各个表现列
        table.set_performance_style(1, background_color=True, text_color=False)
        table.set_performance_style(2, background_color=True, text_color=False)
        table.set_performance_style(3, background_color=True, text_color=False)
        
        return table
    
    def create_heatmap_demo(self):
        """创建热力图风格演示表格"""
        headers = ['产品名称', '销量(万)', '利润率(%)', '市场份额(%)']
        
        # 生成一些模拟数据
        products = ['产品A', '产品B', '产品C', '产品D', '产品E']
        data = []
        for product in products:
            sales = random.randint(10, 100)
            profit = round(random.uniform(5, 25), 1)
            market_share = round(random.uniform(5, 30), 1)
            data.append([product, sales, profit, market_share])
        
        table = TgTable(headers=headers, data=data, auto_height=True)
        
        # 设置热力图风格
        # 销量: 默认的红->白->蓝渐变
        table.set_heatmap_style(1, colors=['#C1272D', '#FFFFFF', '#2E5A87'], background_color=True, text_color=False)
        
        # 利润率(%): 自定义红->黄->绿渐变
        table.set_heatmap_style(2, colors=['#d73027', '#fee08b', '#1a9850'], background_color=True, text_color=False)
        
        # 市场份额(%): 使用预设的 'blue' 主题
        table.set_heatmap_style(3, colors='blue', background_color=True, text_color=False)
        
        return table
    
    def create_gradient_demo(self):
        """创建彩虹渐变风格演示表格"""
        headers = ['学生姓名', '数学成绩', '英语成绩', '综合评分']
        
        # 生成一些模拟数据
        students = ['张三', '李四', '王五', '赵六', '孙七']
        data = []
        for student in students:
            math_score = random.randint(60, 100)
            english_score = random.randint(65, 95)
            total_score = round((math_score + english_score) / 2, 1)
            data.append([student, math_score, english_score, total_score])
        
        table = TgTable(headers=headers, data=data, auto_height=True)
        
        # 设置彩虹渐变风格
        table.set_gradient_style(1, value_range=(0, 100), background_color=True, text_color=False)  # 数学成绩
        table.set_gradient_style(2, value_range=(0, 100), background_color=True, text_color=False)  # 英语成绩
        table.set_gradient_style(3, value_range=(0, 100), background_color=True, text_color=False)  # 综合评分
        
        return table


def main():
    """主函数"""
    app = QApplication(sys.argv)
    
    # 创建演示窗口
    demo = TableColorStyleDemo()
    demo.show()
    
    sys.exit(app.exec())


if __name__ == '__main__':
    main() 